var select_id="";
var status = "";
var availability = "";
$(document).ready(function() { //data_tbody brief
    $("#search_bt").click(function (){
        if ($("#keyword").val()=="Search by Keywords..."){
            $("#keyword").val("");
        }
    });
    $("#keyword").focus(function () {
        if ($("#keyword").val()=="Search by Keywords..."){
            $("#keyword").val("");
        }
    });
    $("#keyword").blur(function () {
        if ($("#keyword").val()==""){
            $("#keyword").val("Search by Keywords...");
        }
    });
    $("#viewquestion").click(function (){
        if (select_id == ""){
            alert("please select one to view!");
            return false;
        }else{
            window.location.href="view.html?id="+select_id+"&from=list";
        }
    });
    $("#editquestion").click(function (){
        if (select_id == ""){
            alert("please select one to edit!");
            return false;
        }else{
            $("#Msg_lock").hide();
            $("#Msg_retire").hide();
            if (status == "retired"){
                $("#Msg_lock").hide();
                $("#Msg_retire").show();
                return false;
            }
            if (availability=="locked"){
                $("#Msg_retire").hide();
                $("#Msg_lock").show();
                return false;
            }
            if (status == "active" && availability=="open"){
                $("#Msg_lock").hide();
                $("#Msg_retire").hide();
                window.location.href="edit.html?id="+select_id;
            }  
        }
    });
    
    $("#newversionquestion").click(function (){
        if (select_id == ""){
            alert("please select one to create new version question!");
            return false;
        }else{
            $("#Msg_lock").hide();
            $("#Msg_retire").hide();
            if (status == "retired"){
                $("#Msg_lock").hide();
                $("#Msg_retire").show();
                return false;
            }            
            if (status == "active"){
                $("#Msg_lock").hide();
                $("#Msg_retire").hide();
                window.location.href="newversion.html?id="+select_id+"&from=list";
            }				
        }
    });
    $("#copyquestion").click(function (){
        if (select_id == ""){
            alert("please select one to copy!");
            return false;
        }else{
            window.location.href="copy.html?id="+select_id+"&from=list";
        }
    });
    $("#retirequestion").click(function (){
        if (select_id == ""){
            alert("please select one to retire!");
            return false;
        }else{
            $("#Msg_lock").hide();
            $("#Msg_retire").hide();
            if (status == "retired"){
                $("#Msg_lock").hide();
                $("#Msg_retire").show();
                return false;
            }
            if (status == "active"){
                $("#Msg_lock").hide();
                $("#Msg_retire").hide();
                window.location.href="retire.html?id="+select_id+"&from=list";
            }
        }
    });
    $("#deletequestion").click(function (){
        if (select_id == ""){
            alert("please select one to delete!");
            return false;
        }else{
            $("#Msg_lock").hide();
            $("#Msg_retire").hide();
            if (availability=="locked"){
                $("#Msg_lock").show();
                $("#Msg_retire").hide();
                return false;
            }
            if (status == "retired"){
                $("#Msg_lock").hide();
                $("#Msg_retire").show();
                return false;
            }
            if (confirm("Are you sure you want to delete this Question?")){                
                if (status == "active" && availability=="open"){
                    $("#Msg_lock").hide();
                    $("#Msg_retire").hide();
                    $.post('delete.html', {
                        id: select_id
                    },
                    function(data) {
                        if (data != ""){                            
                            if (data.indexOf(":true")>0){
                                data = data.replace(":true","");
                                $("#" + select_id).remove();
                                select_id = "";
                                $('.error h3').html(data);
                            }

                        }else{
                            $('.error h3').html("error!");
                        }
                    });
                }
            }else{
                $('.error h3').html("Action cancelled");
            }

        }
    });
});
var Active = function(obj,qid,qStatus,qAvailability){
    $("#Msg_lock").hide();
    $("#Msg_retire").hide();
    displayButton($("#editquestion"));
    displayButton($("#newversionquestion"));
    displayButton($("#deletequestion"));
    displayButton($("#retirequestion"));
    $("div.data_tbody div").removeClass("active")
    var FirstFlag = $(obj).attr("class");
    if (select_id==qid){
        select_id="";
        status = "";        
    }else{
        select_id=qid;
        status = qStatus;
        availability = qAvailability;
        $(obj).toggleClass("active");
        if (qStatus == "active" && qAvailability=="open"){
            displayButton($("#editquestion"));
            displayButton($("#newversionquestion"));
            displayButton($("#retirequestion"));
            displayButton($("#deletequestion"));
        }
        if (qStatus == "retired"){
            greyButton($("#editquestion"));
            greyButton($("#newversionquestion"));
            greyButton($("#retirequestion"));
            greyButton($("#deletequestion"));
        }
        if (qAvailability=="locked"){
            greyButton($("#editquestion"));
            greyButton($("#deletequestion"));
        //greyButton($("#newversionquestion"));
        }
    }
			
}